<template>
  <el-dialog title="抽取设置历史" :visible.sync="show" width="1000px" @closed="reset">
    <el-table v-loading="listLoading" :data="list" size="small" height="calc(88vh - 200px)" style="width: 100%">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="模块" prop="scoreTypeName" sortable align="center" />
      <el-table-column label="类型" align="center">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_random_area_type" :value="row.randomMethod" />
      </el-table-column>
      <el-table-column label="抽检数量" prop="randomCount" />
    </el-table>
    <div slot="footer">
      <el-button type="primary" @click="show = false">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 抽取设置历史
import { getRandomRecordList } from '@/api/projectInspection/courseCheck'
import { getScoreTypeEnum } from './common'
export default {
  props: {
    value: { type: Boolean, default: false },
    processType: { type: String, default: '' },
    projectId: { type: String, default: '' },
    randomId: { type: String, default: '' }
  },
  dicts: ['cq_random_area_type'],
  data() {
    return {
      listLoading: false,
      list: []
    }
  },
  computed: {
    show: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    scoreType2label() {
      return getScoreTypeEnum(this.processType) || {}
    }
  },
  watch: {
    show(val) {
      if (val) this.getList()
    }
  },
  methods: {
    reset() {
      this.listLoading = false
      this.list = []
    },
    async getList() {
      const { projectId, randomId, reset } = this
      if (!projectId || !randomId) return reset()

      this.listLoading = true
      const res = await getRandomRecordList({ projectId, randomId }).catch(() => false)
      this.listLoading = false
      if (res === false) return reset()

      const data = res.data || []
      if (!data.length) return reset()

      let list = []
      data.map(i => {
        try {
          const inputRange = JSON.parse(i.inputRange)
          // const rdcParamDetailVOS = inputRange.rdcParamDetailVOS || []
          const rdcParamVOS = inputRange.rdcParamVOS || []
          list = list.concat(rdcParamVOS)
        } catch (err) {
          console.error('parse inputRange fail', err)
        }
      })
      if (!list.length) return reset()

      list.map(i => {
        i.scoreTypeName = this.scoreType2label[i.scoreType] || ''
      })
      this.list = list
    }
  }
}
</script>
